package es.ats.codington.festival.services;

import java.sql.Connection;
import java.util.ArrayList;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;

import es.ats.codington.festival.daos.DAOManager;

import es.ats.codington.festival.daos.EventSignUpDAOImpl;
import es.ats.codington.festival.daos.PlaceDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.interfaces.services.IEventSingUpService;

/**
 * The Class EventSingUpService.
 *
 * @author 5Cs
 */
public class EventSingUpService implements IEventSingUpService{
	private IEventSignUpDAO eventSignUpDAO;
	
	public EventSingUpService(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.eventSignUpDAO = daoManager.getEventSignUpDAO();
	}
	
	
	/**
	 * Register in event.
	 *
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventSingUpService#registerInEvent(int, int)
	 */
	@Override
	public boolean registerInEvent(int idVisitor, int idEvent) throws Exception{

		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		try{
			
			rs = eventSignUpDAO.registerEventByVisitor(idVisitor, idEvent);

		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		//if (rs) System.out.println(" Event singUp!");
		return rs;
	}
	
	
	
	/**
	 * Un register in event.
	 *
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 */
	@Override
	public boolean unRegisterInEvent(int idVisitor, int idEvent) throws Exception{

		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		//EventSignUpDAOImpl eventDAO = new EventSignUpDAOImpl();
		try{
		
			rs = eventSignUpDAO.unRegisterEventInVisitor(idVisitor, idEvent);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println(" Event singUp!");
		return rs;
	}
	
	
	
	/**
	 * Visitor in event.
	 *
	 * @param idVisitor the id visitor
	 * @param event it's modified if this visitor is registred in any events
	 * 
	 * return true if has registred in any event
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean visitorInEvent(int idVisitor, ArrayList<Event> event) throws Exception{

		int rs = 0;
		int i = 0;
		boolean res = false;
		
		ServiceManager serviceManager = new ServiceManager();
		//EventSignUpDAOImpl eventDAO = new EventSignUpDAOImpl();
		try{

			 res = eventSignUpDAO.getEventsByVisitor(idVisitor, event);
				if (rs>0)res= true; 
			//}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}

		return res;

	}

	/**
	 * Obtain semi all events.
	 *
	 * @param idV the id v
	 * @param event2 the event2
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean obtainSemiAllEvents(int idV, ArrayList<Event> event2) throws Exception{
		int rs = 0;
		int i = 0;
		boolean res = false;
		
		ServiceManager serviceManager = new ServiceManager();
	//	EventSignUpDAOImpl eventDAO = new EventSignUpDAOImpl();
		try{


			 res = eventSignUpDAO.getSemiAllEvents(idV, event2);
				
			//}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}

		return res;
	}
}
